home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / USEFUL_C / DRIVER.C < prev    next >
C/C++ Source or Header  |  1990-04-29  |  5KB  |  259 lines

  1. /****************************************************************************
  2.  *
  3.  *    FILE:    driver.c
  4.  *    CREA:    Sven Axelsson, GU
  5.  *    MODF:    tisdag 12 december 1989 @ 23.33.48
  6.  *    HIST:    89-12-12 (1.0)    First version.
  7.  *
  8.  ****************************************************************************/
  9.  
  10. /****************************************************************************
  11.  This is basically the same code that appeared in Dr. Dobb's Journal,
  12.  Macintosh Issue of Fall 1989, in an article on device drivers by
  13.  Bryan Waters.
  14.  I have found it extremely convenient to use this when writing DAs and
  15.  such, so here is the code for those who can't be bothered with typing
  16.  in themselves.
  17.  I have added the global gRsrcBase for convenience, so that you easily
  18.  can get the DAs owned resources from anywhere in your code without having
  19.  to pass variables or objects around. It is also avaliable as the object
  20.  variable rsrcBase for those who preferres it that way.
  21.  ****************************************************************************/
  22.  
  23.  
  24. # include    "driver.h"
  25.  
  26. # define    OPEN    0
  27. # define    PRIME    1
  28. # define    CONTROL    2
  29. # define    STATUS    3
  30. # define    CLOSE    4
  31.  
  32. driver        *gDrvr = NULL;        /* The driver object */
  33. short        gRsrcBase;            /* Add this offset for owned resources */
  34.  
  35. /******************************************************************************
  36.  main
  37.  ******************************************************************************/
  38.  
  39. short
  40. main(
  41.     CntrlParam        *paramBlock,
  42.     DCtlPtr            devCtlEnt,
  43.     short            routine )
  44. {
  45.     short            result = 0;
  46.     
  47.     if( routine != OPEN && gDrvr != NULL ) {
  48.         gDrvr->pb = paramBlock;
  49.         gDrvr->dc = devCtlEnt;
  50.         gDrvr->async = paramBlock->ioTrap & asyncTrpBit;
  51.         gDrvr->status = 0;
  52.     }
  53.     else if( routine != OPEN )
  54.         return badUnitErr;
  55.         
  56.     switch( routine ) {
  57.         case OPEN:
  58.             if( gDrvr == NULL )
  59.                 if( devCtlEnt->dCtlStorage == NULL )
  60.                     result = -1;
  61.                 else {
  62.                     gDrvr = New();
  63.                     if( gDrvr == NULL )
  64.                         result = -1;
  65.                     else {
  66.                         gDrvr->pb = paramBlock;
  67.                         gDrvr->dc = devCtlEnt;
  68.                         gDrvr->async = paramBlock->ioTrap & asyncTrpBit;
  69.                         gDrvr->drvrRef = devCtlEnt->dCtlRefNum;
  70.                         gDrvr->rsrcBase = 0xC000 | (~gDrvr->drvrRef << 5);
  71.                         gRsrcBase = gDrvr->rsrcBase;
  72.                         
  73.                         gDrvr->Open();
  74.                     }
  75.                 }
  76.             break;
  77.                 
  78.         case PRIME:
  79.             gDrvr->Prime();
  80.             break;
  81.             
  82.         case CONTROL:
  83.             gDrvr->Control();
  84.             break;
  85.             
  86.         case STATUS:
  87.             gDrvr->Status();
  88.             break;
  89.             
  90.         case CLOSE:
  91.             gDrvr->Close();
  92.             result = gDrvr->status;
  93.             delete( gDrvr );
  94.             gDrvr = NULL;
  95.             break;
  96.     }
  97.     
  98.     if( gDrvr != NULL )
  99.         result = gDrvr->status;
  100.         
  101.     return( result );
  102. }
  103.  
  104.  
  105. /******************************************************************************
  106.  Open - METHOD
  107.  ******************************************************************************/
  108.  
  109. void
  110. driver::Open( void )
  111. {
  112.     status = 0;
  113. }
  114.  
  115.  
  116. /******************************************************************************
  117.  Prime - METHOD
  118.  ******************************************************************************/
  119.  
  120. void
  121. driver::Prime( void )
  122. {
  123.     if( pb->ioTrap & 0x00FF == aRdCmd )
  124.         Read();
  125.     else
  126.         Write();
  127. }
  128.  
  129.  
  130. /******************************************************************************
  131.  Read - METHOD
  132.  ******************************************************************************/
  133.  
  134. void
  135. driver::Read( void )
  136. {
  137. }
  138.  
  139.  
  140. /******************************************************************************
  141.  Write - METHOD
  142.  ******************************************************************************/
  143.  
  144. void
  145. driver::Write( void )
  146. {
  147. }
  148.  
  149.  
  150. /******************************************************************************
  151.  Control - METHOD
  152.  ******************************************************************************/
  153.  
  154. void
  155. driver::Control( void )
  156. {
  157.     switch( pb->csCode ) {
  158.         case accRun:
  159.             Idle();
  160.             break;
  161.             
  162.         case goodbye:
  163.             Close();
  164.             break;
  165.     }
  166. }
  167.  
  168.  
  169. /******************************************************************************
  170.  Status - METHOD
  171.  ******************************************************************************/
  172.  
  173. void
  174. driver::Status( void )
  175. {
  176.     status = 0;
  177. }
  178.  
  179.  
  180. /******************************************************************************
  181.  Idle - METHOD
  182.  ******************************************************************************/
  183.  
  184. void
  185. driver::Idle( void )
  186. {
  187. }
  188.  
  189.  
  190. /******************************************************************************
  191.  Close - METHOD
  192.  ******************************************************************************/
  193.  
  194. void
  195. driver::Close( void )
  196. {
  197.     status = noErr;
  198. }
  199.  
  200.  
  201. /******************************************************************************
  202.  Error - METHOD
  203.  ******************************************************************************/
  204.  
  205. void
  206. driver::Error(
  207.     OSErr    err )
  208. {
  209.     status = err;
  210. }
  211.  
  212.  
  213. /******************************************************************************
  214.  Fetch
  215.  ******************************************************************************/
  216.  
  217. char
  218. Fetch(
  219.     DCtlPtr    dc,
  220.     short    *lastChar )
  221. {
  222.     short    data;
  223.     
  224.     asm
  225.     {
  226.         move.l    dc, a1
  227.         move.l    jFetch, a0
  228.         jsr        (a0)
  229.         move.b    d0, data
  230.     }
  231.     *lastChar = data & 0x8000;
  232.     
  233.     return( data & 0x00FF );
  234. }
  235.  
  236.  
  237. /******************************************************************************
  238.  Stash
  239.  ******************************************************************************/
  240.  
  241. short
  242. Stash(
  243.     DCtlPtr    dc,
  244.     char    charRead )
  245. {
  246.     short    data;
  247.     
  248.     asm
  249.     {
  250.         move.l    dc, a1
  251.         move.b    charRead, d0
  252.         move.l    jStash, a0
  253.         jsr        (a0)
  254.         move.b    d0, data
  255.     }
  256.     
  257.     return( data & 0x8000 );
  258. }
  259.